home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d12
/
cgazv5n4.arc
/
STRINGC.H
< prev
next >
Wrap
C/C++ Source or Header
|
1991-09-23
|
2KB
|
52 lines
//--- STRINGC.H -------------------------- Listing 1 -----------
// String class designed for editing
// by Bruce Eckel
//
// (c) 1991 C Gazette: Object code may be used freely. Source
// code may be used as long as authorship and publication are
// acknowledged. Verified for Borland C++.
//--------------------------------------------------------------
#ifndef STRINGC_H_
#define STRINGC_H_
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
class string {
struct piece { // private nested structure
char* str;
piece* next;
int plength; // length of this piece
piece(piece* nxt, char * s) : next(nxt) {
plength = strlen(s);
str = new char[plength + 1];
strcpy(str, s);
}
// break a piece in two by creating a new piece:
void break_piece(int size) {
// insert a piece in the list; start the string at 'size':
next = new piece(next, str + size);
// truncate the original string at 'size':
str[size] = '\0';
plength = size;
}
~piece() { delete str; }
void print() { printf("%s", str); }
};
piece * head, * cursor;
int Length;
void erase_string(); // used in more than one place
void defragment();
public:
string(char * s);
void append(char * s);
void prepend(char* s);
void insert(int position, char* s);
void erase(int position, int count = 1);
int search(char * findstring); // returns position or -1
int length() { return Length; }
void print();
~string() { erase_string(); }
};
#endif // STRINGC_H_